//
//  QRCodeIntegrateViewController.m
//  SDKSample
//
//  Created by 小肥羊 on 15/6/3.
//  Copyright (c) 2015年 Zhu‘s Game House. All rights reserved.
//

#import "QRCodeIntegrateViewController.h"

@interface QRCodeIntegrateViewController ()

@end

@implementation QRCodeIntegrateViewController

#pragma mark - 初始化及设置
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self tapBackground];
}

/*! @brief 关闭小键盘手势 */
-(void)tapBackground
{
    UITapGestureRecognizer * tap =
        [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce)];  //定义一个手势
    [tap setNumberOfTouchesRequired:1];     //触击次数这里设为1
    [self.view addGestureRecognizer:tap];   //添加手势到View中
}
-(void)tapOnce  //手势方法
{
    [self.codeText resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 操作方法
/*! @brief 生成二维码 */
- (IBAction)createCode:(UIButton *)sender {
    self.codeImage.image =
        [QRCodeGenerator qrImageForString:self.codeText.text imageSize:self.codeImage.bounds.size.width];
}

/*! @brief 图片读取二维码 */
- (IBAction)photoCode:(UIButton *)sender {
    self.isCamera = NO;
    UIImagePickerController*pickCtr = [[UIImagePickerController alloc] init];
    pickCtr.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pickCtr.delegate = self;
    pickCtr.allowsEditing = NO;
    [self presentViewController:pickCtr animated:YES completion:nil];
}

/*! @brief 扫一扫二维码 */
- (IBAction)cameraCode:(UIButton *)sender {
    self.isCamera = YES;
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
    reader.readerDelegate = self;
    reader.supportedOrientationsMask = ZBarOrientationMaskAll;
    ZBarImageScanner *scanner = reader.scanner;
    [scanner setSymbology: ZBAR_I25
                   config: ZBAR_CFG_ENABLE
                       to: 0];
    [self presentViewController:reader animated:YES completion:nil];
}

/*! @brief 扫一扫结果 */
- (void)imagePickerController:(UIImagePickerController *)reader didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    id<NSFastEnumeration> results;
    UIImage *image;
    if (self.isCamera) {
        image = [info objectForKey:UIImagePickerControllerEditedImage];
        results = [info objectForKey: ZBarReaderControllerResults];
    } else {
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
        ZBarReaderController* read = [ZBarReaderController new];
        CGImageRef cgImageRef = image.CGImage;
        results = [read scanImage:cgImageRef];
    }

    ZBarSymbol *symbol = nil;
    for(symbol in results)
        break;
    self.codeImage.image = image;
    [reader dismissViewControllerAnimated:YES completion:nil];
    
    //判断是否包含 头'http:'
    NSString *regex = @"http+:[^\\s]*";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    
    //判断是否包含 头'ssid:'
    NSString *ssid = @"ssid+:[^\\s]*";;
    NSPredicate *ssidPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",ssid];
    
    self.resultLabel.text =  symbol.data ;
    if ([predicate evaluateWithObject:self.resultLabel.text]) {
        
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil
                                                        message:@"It will use the browser to this URL。"
                                                       delegate:nil
                                              cancelButtonTitle:@"Close"
                                              otherButtonTitles:@"Ok", nil];
        alert.delegate = self;
        alert.tag=1;
        [alert show];
    }
    else if([ssidPre evaluateWithObject:self.resultLabel.text]){
        
        NSArray *arr = [self.resultLabel.text componentsSeparatedByString:@";"];
        NSArray * arrInfoHead = [[arr objectAtIndex:0] componentsSeparatedByString:@":"];
        NSArray * arrInfoFoot = [[arr objectAtIndex:1] componentsSeparatedByString:@":"];
        
        self.resultLabel.text=
        [NSString stringWithFormat:@"ssid: %@ \n password:%@",
         [arrInfoHead objectAtIndex:1],[arrInfoFoot objectAtIndex:1]];
        
        
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:self.resultLabel.text
                                                        message:@"The password is copied to the clipboard , it will be redirected to the network settings interface"
                                                       delegate:nil
                                              cancelButtonTitle:@"Close"
                                              otherButtonTitles:@"Ok", nil];
        alert.delegate = self;
        alert.tag=2;
        [alert show];
        
        UIPasteboard *pasteboard=[UIPasteboard generalPasteboard];
        //        然后，可以使用如下代码来把一个字符串放置到剪贴板上：
        pasteboard.string = [arrInfoFoot objectAtIndex:1];
    }
}


- (IBAction)DidEndOnExit:(UITextField *)sender {
    [sender resignFirstResponder]; 
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
